package com.party.service.exam.impl;

import com.party.common.Page;
import com.party.mapper.ExamAnswerMapper;
import com.party.mapper.ExamTestMapper;
import com.party.mapper.User2ExamMapper;
import com.party.model.*;
import com.party.service.exam.ExamAnswerService;
import com.party.service.exam.ExamTestService;
import com.party.service.exam.User2ExamService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-9
 * Time: 下午8:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class User2ExamServiceImpl implements User2ExamService {

    @Resource
    User2ExamMapper user2ExamMapper;
    @Override
    public int add(User2Exam obj) {
        if(obj != null)
        {
            return this.user2ExamMapper.insertSelective(obj);
        }
        return 0;
    }

    @Override
    public User2Exam get(Integer uid, Integer examid) {
        if(uid != null && uid >0 && examid != null && examid > 0)
        {
            User2ExamExample example = new User2ExamExample();
            User2ExamExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(uid);
            criteria.andExamidEqualTo(examid);
            List<User2Exam> user2ExamList = this.user2ExamMapper.selectByExample(example);
            if(user2ExamList != null && user2ExamList.size() >0)
            {
                return user2ExamList.get(0);
            }
        }
        return null;
    }

    @Override
    public int update(User2Exam user2Exam) {
        if(user2Exam != null && user2Exam.getId() != null && user2Exam.getId() > 0)
        {
            return this.user2ExamMapper.updateByPrimaryKeySelective(user2Exam);
        }
        return 0;
    }

    @Override
    public Page getList(User2Exam user2Exam, int pageSize, int pageIndex) {
        if(user2Exam == null )
        {
            user2Exam = new User2Exam();
            user2Exam.setRealname("");
            user2Exam.setName("");
            user2Exam.setPassport("");
            user2Exam.setqMaxScore(Integer.MAX_VALUE);
            user2Exam.setqMinScore(-1);
        }
        else
        {
            if(user2Exam.getName() == null || "".equals(user2Exam.getName()))
            {
                user2Exam.setName("");
            }
            if(user2Exam.getRealname() == null || "".equals(user2Exam.getRealname()))
            {
                user2Exam.setRealname("");
            }
            if(user2Exam.getPassport() == null || "".equals(user2Exam.getPassport()))
            {
                user2Exam.setPassport("");
            }
            if(user2Exam.getqMaxScore() == null)
            {
                user2Exam.setqMaxScore(Integer.MAX_VALUE);
            }
            if(user2Exam.getqMinScore() == null)
            {
                user2Exam.setqMinScore(-1);
            }
        }
        //获取总的记录数量
        int count = this.user2ExamMapper.countByUser2Exam(user2Exam);
        //获取指定的页码数据
        user2Exam.setBeginNum((pageIndex - 1) * pageSize);
        user2Exam.setEndNum(pageSize);
        List<User2Exam> list = this.user2ExamMapper.getListPage(user2Exam);
        //组合返回page
        Page page = new Page(pageIndex, count, pageSize, list);
        return page;
    }


}
